package com.ymatou.validatecode.api.testcase;

import java.util.Calendar;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.validatecode.api.parameter.GetValidateCodeBean;
import com.ymatou.validatecode.api.parameter.SetValidationCodeInvalidBean;
import com.ymatou.validatecode.api.service.GetValidateCodeCall;
import com.ymatou.validatecode.api.service.SetValidationCodeInvalidCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ValidateCodeService;
import com.ymttest.database.model.YmtValidationCode;
import com.ymttest.database.sqlwapper.YmtValidationCodeWapper;
import com.ymttest.utils.TestDataManager;
/**
*设置验证码失效用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SetValidationCodeInvalid{
	private static SetValidationCodeInvalidBean setvalidationcodeinvalidBean;
	private static SetValidationCodeInvalidCall setvalidationcodeinvalidCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置验证码失效用例");
	}

	@Before
	public void caseUp() {
		setvalidationcodeinvalidBean=new SetValidationCodeInvalidBean();
		setvalidationcodeinvalidCall=new SetValidationCodeInvalidCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setvalidationcodeinvalidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetValidationCodeInvalid_001() {
		Logger.start(true,"设置验证码失效-未过期");
		try {
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeService().setGetValidateCodeBean();
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeService().GetValidateCode(getvalidatecodeBean);
		
			setvalidationcodeinvalidBean.setActionUser(getvalidatecodeBean.getActionUser());
			setvalidationcodeinvalidBean.setValidateType(getvalidatecodeBean.getValidateType());
			
	        Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();

	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 1); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			new YmtValidationCodeWapper().setExpiredTime(getValidateCodeCall.getValidateCode(), dAfter);
			
			setvalidationcodeinvalidCall.setData(setvalidationcodeinvalidBean);
			setvalidationcodeinvalidCall.callService();			
			
			Logger.verifyEquals("200", setvalidationcodeinvalidCall.getCode(), "验证返回code");
			Logger.verifyEquals("", setvalidationcodeinvalidCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("true", setvalidationcodeinvalidCall.getIsSuccess(), "验证返回Message");
			// 验证数据库落地正确
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(getvalidatecodeBean.getActionUser(), getvalidatecodeBean.getValidateType()).get(0);
			Logger.verifyEquals(getValidateCodeCall.getValidateCode(), validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(-1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(0, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetValidationCodeInvalid_002() {
		Logger.start(true,"设置验证码失效-已过期");
		try {
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeService().setGetValidateCodeBean();
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeService().GetValidateCode(getvalidatecodeBean);
		
			setvalidationcodeinvalidBean.setActionUser(getvalidatecodeBean.getActionUser());
			setvalidationcodeinvalidBean.setValidateType(getvalidatecodeBean.getValidateType());
			
	        Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();

	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, 1); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			new YmtValidationCodeWapper().setExpiredTime(getValidateCodeCall.getValidateCode(), dBefore);
			
			setvalidationcodeinvalidCall.setData(setvalidationcodeinvalidBean);
			setvalidationcodeinvalidCall.callService();			
			
			Logger.verifyEquals("200", setvalidationcodeinvalidCall.getCode(), "验证返回code");
			Logger.verifyEquals("", setvalidationcodeinvalidCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("true", setvalidationcodeinvalidCall.getIsSuccess(), "验证返回Message");
			// 验证数据库落地正确
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(getvalidatecodeBean.getActionUser(), getvalidatecodeBean.getValidateType()).get(0);
			Logger.verifyEquals(getValidateCodeCall.getValidateCode(), validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(0, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(0, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}